#include "SchoolBattle.h"
#include "Game.h"

SchoolBattle::SchoolBattle()
{
	Init();
}

void SchoolBattle::Init()
{
	m_CurrentDay = DAYS_MONDAY;
	m_CurrentDayTime = DAYTIME_MORNING;

	m_AIPlayer = 0;
	m_Player = 0;

	//RANDOM SEED
	srand(time(NULL));
}

void SchoolBattle::Update()
{
	//DEBUG_LOG("Current State: %s\n", Game::GetState()->GetStateName());
	Game::GetState()->Update();
}

void SchoolBattle::Render()
{
	if(Game::isUpdateCalled)
		Game::GetState()->Render();
	else
		Game::isUpdateCalled = true;
}

int SchoolBattle::GetCurrentDay()
{
	return m_CurrentDay;
}

void SchoolBattle::NextDay()
{
	m_Player->SetCurrentDayPoints(DEFAULT_DAILY_POINT); //This will set the points for the next day. Go compute when user is sleep deprived.
	m_CurrentDay++;
	if(m_CurrentDay >= DAYS_COUNT)
		m_CurrentDay = 0;
}

void SchoolBattle::GenerateAIPlayer(core::stringw aiName, int mainAttrib, int rank)
{
	//srand(time(NULL));
	if(m_AIPlayer != NULL)
		SAFE_DEL(m_AIPlayer);
	int randAttrib = rand() % 3;
	m_AIPlayer = new AIPlayer(aiName, randAttrib, rank);
	m_AIPlayer->SetPlayerSetting();
	DEBUG_LOG("NAME: %s | MAIN_ATTRIB: %d | RANK: %d\n", aiName, randAttrib, rank);
}

void SchoolBattle::Attack(Player* pAttack, Player* pReceive)
{
	int min_dmg =  pAttack->GetAtk() - RANGE_DAMAGE_VALUE;
	int max_dmg =  pAttack->GetAtk() + RANGE_DAMAGE_VALUE;
	int range_dmg = max_dmg - min_dmg;
	int atk = rand() % range_dmg + min_dmg;

	pReceive->DamageTaken(atk);

	DEBUG_LOG("PlayerAtk: %s | PlayerRec: %s | ATTACK GENERATED: %d\n", pAttack->GetPlayerName(), pReceive->GetPlayerName(), atk);
}

void SchoolBattle::CreatePlayer(core::stringw name, int mainAttrib, int rank)
{
	if(m_Player != NULL)
		SAFE_DEL(m_Player);
	m_Player = new Player(name, mainAttrib, rank);
}

Player* SchoolBattle::GetPlayer()
{
	return m_Player;
}

AIPlayer* SchoolBattle::GetEnemyPlayer()
{
	return m_AIPlayer;
}

void SchoolBattle::Reset()
{
	m_CurrentDay = DAYS_MONDAY;
}

int SchoolBattle::GetDayTime()
{
	return m_CurrentDayTime;
}

void SchoolBattle::NextDayTime()
{
	m_CurrentDayTime++;
	if(m_CurrentDayTime >= DAYTIME_COUNT)
		m_CurrentDayTime = DAYTIME_MORNING;
}